package com.zd.lib.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.text.TextUtils;


public class SPUtils {

	private static Context mContext;
	/**
	 * 保存String
	 */
	public static void setString2SP(Context context,String key, String value) {
		if (TextUtils.isEmpty(key) || value == null) {
			return;
		}
		if (mContext == null) {
			mContext=context;
		}
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
		Editor editor = sp.edit();
		editor.putString(key, value);
		editor.commit();
	}
	public static void setString2SP( String key, String value) {
		if (TextUtils.isEmpty(key)) {
			return;
		}
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
		Editor editor = sp.edit();
		editor.putString(key, value);
		editor.commit();
	}
	/**
	 * 获取String
	 */
	public static String getStringFromSP(Context context,String key, String defaultValue) {
		if (TextUtils.isEmpty(key)) {
			return defaultValue;
		}
		if (mContext == null) {
			mContext=context;
		}
		SharedPreferences sp =  PreferenceManager.getDefaultSharedPreferences(mContext);
		return sp.getString(key, defaultValue);
	}
	public static String getStringFromSP(String key, String defaultValue) {
		if (TextUtils.isEmpty(key)) {
			return defaultValue;
		}
		SharedPreferences sp =  PreferenceManager.getDefaultSharedPreferences(mContext);
		return sp.getString(key, defaultValue);
	}

	/**
	 * 保存int
	 */
	public static void setString2SP(Context context,String key, int value) {
		if (TextUtils.isEmpty(key)) {
			return;
		}
		if (mContext == null) {
			mContext=context;
		}
		SharedPreferences sp =  PreferenceManager.getDefaultSharedPreferences(mContext);
		Editor editor = sp.edit();
		editor.putInt(key, value);
		editor.commit();
	}

	/**
	 * 获取int
	 */
	public static int getIntFromSP(Context context,String key, int defaultValue) {
		if (TextUtils.isEmpty(key)) {
			return defaultValue;
		}
		if (mContext == null) {
			mContext=context;
		}
		SharedPreferences sp =  PreferenceManager.getDefaultSharedPreferences(mContext);
		return sp.getInt(key, defaultValue);
	}

}
